import os, fitz, re, ini_op, duckdb, pytesseract
from pdf2image import convert_from_path


# 指定 Tesseract-OCR 的路径
pytesseract.pytesseract.tesseract_cmd = r"D:\tool\Tesseract OCR\tesseract.exe"
poppler_path = r'D:\tool\poppler-24.08.0\Library\bin'
def sortAndRename(conn, pdf_path, output_folder, ht_key, pz_key, pzrq_method, pzh_method, keep_ht, hb_doc):
    doc = fitz.open()
    new_doc = fitz.open(pdf_path)
    images = []
    ht_pages = []
    pz_pages = []
    other_pages = []
    date_value = '未找到凭证页'
    voucher_value = '未找到凭证页'

    is_ht_page = False
    is_pz_page = False
    # 遍历PDF每一页，寻找包含关键词的页
    for i in range(len(new_doc)):
        text = new_doc[i].get_text("text").replace(" ", "")

        if not text.strip():  # 如果没有文字，尝试OCR
            if not images:
                images = convert_from_path(pdf_path, poppler_path=poppler_path)
            print(f"第 {i + 1} 页没有文字，尝试OCR")
            text = pytesseract.image_to_string(images[i], lang="chi_sim").replace(" ", "")

        if ht_key in text:
            is_ht_page = True
        # 处理凭证页
        if pz_key in text:
            is_ht_page = False
            is_pz_page = True
            pz_pages.append(i)
            # 正则匹配日期与凭证号
            date_pattern = pzrq_method
            voucher_pattern = pzh_method

            # 提取匹配的内容
            date_match = re.search(date_pattern, text)
            voucher_match = re.search(voucher_pattern, text)

            # 获取匹配的值
            date_value = date_match.group(1) if date_match else "未找到日期"
            voucher_value = voucher_match.group(1) if voucher_match else "未找到凭证号"
        elif is_ht_page:
            ht_pages.append(i)
        elif is_pz_page:
            pz_pages.append(i)
        else:
            other_pages.append(i)
    # 重新排列页面
    if keep_ht:
        all_pages = pz_pages + ht_pages + other_pages
    else:
        all_pages = pz_pages + other_pages
    for page_idx in all_pages:
        doc.insert_pdf(new_doc, from_page=page_idx, to_page=page_idx)
        hb_doc.insert_pdf(new_doc, from_page=page_idx, to_page=page_idx)
    #保存新pdf
    if date_value == '未找到凭证页':
        filename = f"{os.path.basename(pdf_path)[:-4]}('未找到凭证页').pdf"
    else:
        filename = f"{date_value[:4]}-{date_value[4:6]}-{voucher_value}.pdf"
    output_path = os.path.join(output_folder, filename)
    pdf_path = os.path.join(os.path.dirname(pdf_path), '联合打印-'+filename)
    doc.save(output_path)
    doc.save(pdf_path)
    conn.execute("delete from t_pdf_sort where filepath = ?", [output_path])
    conn.execute("insert into t_pdf_sort (fileName, filepath, year, month, pzh) values (?,?,?,?,?)", [filename,output_path,date_value[:4],date_value[4:6],voucher_value])
    new_doc.close()
    doc.close()
    print(f"处理完成，新的PDF已保存至 {output_path}")
    return hb_doc

def run(id, path):
    pdf_folder, output_folder, ht_key, pz_key, pzrq_method, pzh_method, keep_ht = ini_op.getinivalue("pdf_config", 
        "pdf_folder", "output_folder", "ht_key", "pz_key", "pzrq_method", "pzh_method", "keep_ht")
    pdf_folder = os.path.join(pdf_folder, id)
    os.makedirs(pdf_folder, exist_ok=True)
    os.makedirs(output_folder, exist_ok=True)
    keep_ht = keep_ht == 'True'
    conn = duckdb.connect("data.db")
    file_list = conn.execute("select filepath from t_pdf_download where parentId = ?", [id]).fetchall()
    hb_doc = fitz.open()
    for file in file_list:
        pdf_path = file[0]
        hb_doc = sortAndRename(conn, pdf_path, output_folder, ht_key, pz_key, pzrq_method, pzh_method, keep_ht, hb_doc)
    hb_doc.save(os.path.join(path, "联合打印.pdf"))
    conn.execute("insert into t_pdf_download (parentId, fileName, filepath) values (?, ?, ?)", [id, "联合打印.pdf", os.path.join(path, "联合打印.pdf")])
    conn.execute("update t_import_files set status = '已执行' where id = ?", [id])
    conn.close()
    # print(f"处理完成，合并PDF已保存至 {os.path.join(output_folder, "联合打印.pdf")}")

if __name__ == "__main__":
    id = '20250317174820'
    run(id)






